package top.fengyulou.xiaofeng.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import top.fengyulou.xiaofeng.common.UuidUtil;
import top.fengyulou.xiaofeng.domain.Material;
import top.fengyulou.xiaofeng.domain.Paper;
import top.fengyulou.xiaofeng.dto.MaterialDto;
import top.fengyulou.xiaofeng.dto.PaperDto;
import top.fengyulou.xiaofeng.dto.QuestionDto;
import top.fengyulou.xiaofeng.mapper.MaterialMapper;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author guochao
 * @version 1.0
 * @date 2022/1/27 10:27
 */
@Service
public class MaterialService {

    @Resource
    private MaterialMapper materialMapper;

    public List<MaterialDto> list(MaterialDto materialDto){
        return materialMapper.list(materialDto);
    }

    /**
     * 列表查询
     */
    public void listPage(MaterialDto query) {
        //查询数据
        PageHelper.startPage(query.getPageNumber(), query.getPageSize());
        List<MaterialDto> list = materialMapper.list(query);
        PageInfo<MaterialDto> pageInfo = new PageInfo<>(list);

        //返回结果
        query.setTotal(pageInfo.getTotal());
        query.setRows(list);
    }

    //保存
    public void save(Material material) {
        if (StringUtils.isNotBlank(material.getId())) {
            //修改
            material.setUpdateTime(new Date());
            material.setLastUpdateUserId(material.getCurrentUserId());
            materialMapper.update(material);
        } else {
            //添加
            material.setId(UuidUtil.getShortUuid());
            material.setCreateTime(new Date());
            material.setCreateUserId(material.getCurrentUserId());
            materialMapper.insert(material);
        }
    }

    //删除
    public void delete(String[] ids) {
        materialMapper.delete(ids);
    }

    //根据id查询
    public MaterialDto getById(String id) {
        return materialMapper.getById(id);
    }
}
